#include<stdio.h>
#include<stdlib.h>
#include<at89c51ed2.h>
#include <mcs51reg.h>
#include<ctype.h>
#include "lab3supp.h"



void _sdcc_external_startup() {
	AUXR|=0x0C;	//XRAM 1024 bytes activated
	TMOD|=0x25;		//Timer 1 Mode 2
	TH1=0xFD;		// Timer Value 253
	SCON = 0x50; // SCON: mode 1, 8-bit UART, enable rcvr
	PCON|=0x80;		// double baud rate to 19200
	TR1 = 1; // TR1: timer 1 run
	TI = 1;	//set TI flag for putchar sequence
	RI = 0;	
	
}

void idleBack(void) __interrupt (2) //low level triggered interrupt
{
	
	//printf_tiny("Welcome back!!");
	togglePin();
	togglePin();
}



void main(){
	
	int modeS=0; //x2 mode identifier
	int pwm=0; //PWM identifer
	int pd=0; //powerdown
	int toggle=0; //counts number of toggle events for verification
	char val;
	int Count;

	printf("Enter Option\r\n");
	newLine();
	Count=0;
	
	
	while(1)
	{
			
			val = getchar();    //take user character input
			
			//putchar(val); //echo characters back
			
			val=toupper(val); //convert to upper characters
			
			switch(val)
			{
				case 'T':		togglePin();
								toggle++;
								break;
								
				case '1':		TR0=1;
								printf_tiny("Counter 0 Started");
								break;
				case '2':		//clear counter
								TH0=0;
								TL0=0;
								Count=0;
								printf_tiny("Counter 0 Cleared");
								break;					
				case '3': 		//Stop Counter
								TR0=0;
								printf_tiny("Counter 0 Stopped");
								break;					
				case '4':		//print the count value
								Count=TH0*256+TL0;
								printf("Counter 0:%d\r\n",Count);
								newLine();
					 			break;		
				case 'P':		//enable PWM, see note on pg41 of datasheet
								if(pwm==0)
								{								
									CMOD=0X02; //PCA clk=Cperiph/2
									CL=0xFF; //start with overflow
									//CCAP0L=0x00; //making lower counter zero
									//CCAP1L=0x00;
									CCAP0H=0xE5; //configured for 10% duty cycle on CEX0 pin, ECOM will be set automatically
									CCAP1H=0x3F;//configured for 75% duty cycle on CEX1 pin,ECOM will be set automatically
									CCAPM0|=0x42; //set ECOM and PWM for Module0
									CCAPM1|=0x42; //set ECOM and PWM for Module1
									//PWM=0x01;
									//ECOM=0x01;
									CCON=0x40; //CR=1, CF=0
									pwm=1;
									printf_tiny("PWM Started: 10% on CEX0/P1.3 75% on CEX1/P1.4");
								}
								else{
									//disable PWM 
									CCON=0x00; //CR=1, CF=0
									CCAPM0&=0xBD;
									CCAPM1&=0xBD;
									//PWM=1;
									//ECOM=1;
									pwm=0;
									printf_tiny("PWM Stopped");
								}
					 			break;	
					 			
					 			
			 	case 'X':		
							 	if (modeS==0){
						 			modeS=1; //x2 mode identifier bit set
						 			CKCKON0=0x25; //PCAX2=12clk,timer 1 clf speed=12,x2 mode set
						 			printf_tiny("X2 Mode ON\r\n");//X mode
						 		}
						 		else{
						 			modeS=0; //x2 mode identifer bit cleared
						 			CKCKON0=0x00; //disable X2 mode		
						 			printf_tiny("X2 Mode OFF\r\n");//X mode	 		
						 		}				 
								 break;	
								 	
				case 'I':		EA=1;
								EX1=1;
								printf("Sending System to Idle Mode\r\n");//idle mode-xlk to CPU Disconnected
								TR0=0;
								//printf("%d:%d",TH0,TL0);
								PCON|=0x01; //set IDL bit to enter idle mode
								printf("\r\nResuming Operations\r\n");//level triggered only(default setting)
								//printf("%d:%d",TH0,TL0);
								TR0=1;
								break;
								
				case 'D':		printf("Entering Power Down Mode\r\n");//power down mode
								PCON|=0x02; //set PD bit to enter power down mode
								break;
								
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			}
			printf_tiny("\ttoggle value %d\r\n",toggle);
				
			
			
	}
}



	
	
	
	
	
	
	

			
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	



	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

